package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Rect
import android.support.annotation.NonNull
import android.support.annotation.Nullable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.AbsoluteLayout
import android.widget.FrameLayout
import com.bjtime.designer.R
import com.bjtime.designer.attributes.BaseAttr
import com.bjtime.designer.attributes.ViewAttr
import java.lang.RuntimeException


/**
 * 所有标签组件的基类.
 */

open class BaseLabelView @JvmOverloads constructor (
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout (context, attrs, defStyleAttr) {

    var des_id      : Int = -1
    var des_left    : Int = 0
    var des_top     : Int = 0
    var des_width   : Int = 0
    var des_height  : Int = 0

    // 打印类型,默认为指令打印0.
    var des_ptype   : ViewAttr.PRINT_TYPE = ViewAttr.PRINT_TYPE.PRINT;
    // 旋转角度,默认为0
    var des_rotate  : Int = 0
    // 是否锁定,默认为假.
    var des_lock    : Boolean = false
    // 是否被选中
    var des_select  : Boolean = false

    var des_viewtype : ViewAttr.VIEW_TYPE = ViewAttr.VIEW_TYPE.BASE

    var des_reversed    : Boolean = false

    // 实际打印位置,  去掉了拖拉边框以及padding. 单位为MM
    var prt_left    : Int = 0
    var prt_top     : Int = 0
    var prt_right   : Int = 0
    var prt_bottom  : Int = 0

    init {

        if (attrs != null) {
            var a = context.obtainStyledAttributes(attrs, R.styleable.BaseLabelView)
            des_lock = a.getBoolean(R.styleable.BaseLabelView_lockpos, des_lock)
            a.recycle()
        }

    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        onSizeChange(w, h, oldw, oldh)
        des_id = des_id
        des_left = left
        des_top = top
        des_width = width
        des_height = height
    }

    open fun onSizeChange (w: Int, h: Int, oldw: Int, oldh: Int) {

    }

    open fun onMoved (spac:Int) {
        des_id = des_id
        des_left = left
        des_top = top
        des_width = width
        des_height = height
    }

    open fun setSelect (select : Boolean) {
        des_select = select
    }

    /***
     * 返回标签属性.JSON->LabelAttr->User.
     * 所有子类都必须重写本方法,并返回属性值。
     */
    open  fun getAttr () : BaseAttr {
        return null as BaseAttr
    }


    open fun setAttr (attr:BaseAttr?) {
        des_id = attr!!.des_id
        des_viewtype = attr!!.des_viewtype
        des_left = attr!!.des_left
        des_top = attr!!.des_top
        des_width = attr!!.des_width
        des_height = attr!!.des_height
        des_ptype = attr!!.des_ptype
        des_rotate = attr!!.des_rotate
        des_lock = attr!!.des_lock
        des_reversed = attr!!.des_reversed
    }

    /**
     * 返回实际组件基于设计器的矩形区域.
     */


    open fun setRect (r : Rect) {
        des_left = r.left
        des_top = r.top
        des_width = r.right
        des_height = r.bottom
    }
    open fun getRect () : Rect {
        // super.left + padding + margin
        return Rect (des_left,des_top,des_width,des_height)
    }

    open fun prepared () {

    }
}